﻿using System;
using System.Collections.Generic;
using System.Text;
using Cn.Cellula.Biz.Port;
using Cn.Cellula.Model;
using Cn.Cellula.Model.Content;

namespace Cn.Cellula.Biz.Impl {
    class VoteBiz : MyBizSupport, IVoteBiz {
        #region IVoteBiz 成员

        public IList<TVoteItemEO> LoadItemsByVote(string rVoteKey) {
            String hql = "from TVoteItemEO eo where eo.VoteKey='" + rVoteKey + "' order by eo.OrderNumber desc ";
            return base.Find<TVoteItemEO>(hql);
        }

        public IList<VVoteEditListEO> LoadAllVote(string rOwnerKey) {
            String hql = "from VVoteEditListEO eo where eo.OwnerKey='" + rOwnerKey + "' order by eo.VoteTitle ";
            return base.Find<VVoteEditListEO>(hql);
        }

        public void DeleteVote(IList<string> rKeys,String rOwnerKey) {
            foreach (String k in rKeys) {
                String hql = "from TVoteEO eo where eo.OwnerKey='" + rOwnerKey + "' and eo.VoteKey='"+k+"' ";
                base.Delete(hql);
                hql = "from TVoteItemEO eo where eo.VoteKey='" + k + "' ";
                base.Delete(hql);
            }
        }

        public void DeleteVoteItem(IList<string> rKeys) {
            foreach (String k in rKeys) {
                String hql = "from TVoteItemEO eo where eo.ItemKey='" + k + "' ";
                base.Delete(hql);
            }
        }

        public void OpenVote(IList<string> rKeys) {
            foreach (String k in rKeys) {
                TVoteEO eo = base.Load<TVoteEO>(k);
                eo.StartDate = DateTime.Now;
                eo.EndDate = null;
                base.Update<TVoteEO>(eo);
            }
        }

        public void CloseVote(IList<string> rKeys) {
            foreach (String k in rKeys) {
                TVoteEO eo = base.Load<TVoteEO>(k);
                eo.EndDate = DateTime.Now;
                base.Update<TVoteEO>(eo);
            }
        }

        public Boolean IfVoted(string rVoteKey, string rUserId, string rIp) {
            String hql = " from TVoteLogEO eo ";
            if (String.IsNullOrEmpty(rUserId)) {//ip策略
                hql += " where eo.FromIp='" + rIp + "' ";
            } else {
                hql += " where eo.Operator='" + rUserId + "' ";
            }
            IList<TVoteLogEO> list = base.Find<TVoteLogEO>(hql);
            return (list != null && list.Count > 0);
        }

        #endregion
    }
}
